#!/bin/sh

MODDIR=/lib/modules
insmod $MODDIR/aotom.ko

if [ -e /var/etc/.firstboot ]; then
   #fp_control does not work here
   echo "WAIT 1 min" >/dev/vfd
   echo "WAIT 1 min"
   rm /var/etc/.firstboot
   touch /dev/.devfsd
fi

/etc/init.d/mountvirtfs start

/etc/init.d/portmap start

insmod $MODDIR/e2_proc.ko

# start automounter with umount timeout of 5 seconds
/usr/sbin/automount --timeout=5 /autofs file /etc/auto.usb

if [ ! -e /var/etc/.firstboot ]; then

   echo "Starting DEBUG Interface"
   /etc/init.d/mountall start
   /etc/init.d/hostname start

   fp_control -c

   echo "starting telnetd"
   /usr/sbin/telnetd -l /bin/login

   echo "init stmfb"
   insmod $MODDIR/stmcore-display-sti7105.ko
   insmod $MODDIR/stmfb.ko display0=1280x720-32@50:15m:pal:yuv:yuv;# display1=720x576-16@50:1m:pal:cvbs;

   echo "LOADING MODULES"   
   fp_control -t "LOADING MODULES"

   if [ ! -e /boot/audio.elf ]; then
      fp_control -t "FW MISSING!!!"
   fi

   echo "load a firmware"
   /bin/ustslave /dev/st231-1 /boot/audio.elf
   echo "load v firmware"
   /bin/ustslave /dev/st231-0 /boot/video.elf

   echo "init embx"
   insmod $MODDIR/embxshell.ko
   insmod $MODDIR/embxmailbox.ko mailbox0=0xfe211000:136:set2 mailbox1=0xfe212000:0 ;
   insmod $MODDIR/embxshm.ko mailbox0=shm:0:7:0x60000000:0:256:256:0:512:0x40000000:0x10000000 ;
   insmod $MODDIR/mme_host.ko transport0=shm

   echo "init AVS"
   insmod $MODDIR/avs.ko type=spark7162_avs

   player=`cat /proc/stb/player/version`
   if [ $player=='player179' ]; then
      echo "init player 179"
   else
      echo "init player 131"
   fi
   insmod $MODDIR/mmelog.ko
   insmod $MODDIR/ksound.ko
   insmod $MODDIR/p2div64.ko
   insmod $MODDIR/stm_v4l2.ko
   if [ $player=='player179' ]; then
      insmod $MODDIR/stmvout.ko
      insmod $MODDIR/stmvbi.ko
   fi
   insmod $MODDIR/pseudocard.ko

   insmod $MODDIR/spark7162.ko
   #insmod $MODDIR/starci.ko 
   if [ $player=='player179' ]; then
      insmod $MODDIR/pti.ko waitMS=20 videoMem=4096
      insmod $MODDIR/stm_monitor.ko
      insmod $MODDIR/stmsysfs.ko
      insmod $MODDIR/stmdvb.ko
   else
      insmod $MODDIR/pti.ko
      insmod $MODDIR/stmdvb.ko
      insmod $MODDIR/stm_monitor.ko
   fi
   insmod $MODDIR/player2.ko
   insmod $MODDIR/sth264pp.ko
   insmod $MODDIR/stmalloc.ko
   insmod $MODDIR/platform.ko
   insmod $MODDIR/silencegen.ko

   insmod $MODDIR/bpamem.ko
   mknod /dev/bpamem c 153 0 

   showiframe -p /boot/bootlogo.mvi &
   stfbcontrol a 0

   echo "init frontpanel"
   insmod $MODDIR/simu_button.ko

   mknod /dev/rc c 147 1 

   #params:
   #period
   #delay
   
   /usr/bin/lircd
   
   /bin/evremote2 10 120 &

   # timefix e2
   # date -s "121220001970"

   stfbcontrol a 255

   /etc/init.d/mountall start

   /etc/init.d/hostname start

   echo "starting telnetd"
   /usr/sbin/telnetd -l /bin/login
   /etc/init.d/vsftpd start
   /usr/sbin/inetd
	mknod /dev/dvb/adapter0/frontend1 c 212 19
	mknod /dev/dvb/adapter0/frontend2 c 212 35
	mknod /dev/dvb/adapter0/frontend3 c 212 51
	
   NFS=$(grep -c 'nfs' /proc/cmdline)
   if [ "$NFS" -eq "1" ]; then 
      echo "Booting from nfs, don't set network" 
   else 
      if [ -e /etc/network/interfaces ]; then 
	 ip addr flush eth0 
	 /etc/init.d/networking stop 
	 TEST=$(grep -c 'dhcp' /etc/network/interfaces) 
	 if [ "$(TEST)" -eq "1" ]; then 
	    echo dhcp
            /etc/init.d/udhcpc start 
	 fi 
	 /etc/init.d/networking start 
      fi 
   fi

   echo "LOADING e2" > /dev/vfd
   echo "LOADING e2"

   until false
   do
      echo "starting e2->"
      /usr/local/bin/enigma2
      rtv=$?
      echo "e2 ended <- RTV: " $rtv
      case "$rtv" in
		0) echo "0"
		   echo "E2 ERROR"
      	   init 6;;
		1) echo "1"
		   echo "SHUTDOWN"
		   init 0;;
		2) echo "2"
		   echo "REBOOT"
		   init 6;;
		3) echo "3"
		   echo "E2 RESTART"
		   echo "0" > /proc/progress
		   showiframe -p /boot/bootlogo.mvi &;;
		*) echo "*"
		   echo "ERROR"
           init 6;;
      esac
   done 
fi

#never reached
echo "REBOOT ATEVIO7500" > /dev/vfd
echo "REBOOT ATEVIO7500"
reboot -f
